﻿using System;
using System.Text;

namespace X.Pager.List
{
    public class MyPager : MyPagerBase
    {
        private int count = 0;                      //总记录数
        private int pageIndex = 0;                  //当前页
        private int currentPgGroup = 0;             //当前页数分组
        private int customerPageSize = 0;           //自定义每页记录数
        private int defaultPagesInGroup = 0;

        public MyPager(int _count, int _pageIndex, int _currentPgGroup, int _customerPageSize, int _defaultPagesInGroup)
        {
            count = _count;
            if (_pageIndex > 0)
                pageIndex = _pageIndex;
            currentPgGroup = _currentPgGroup;
            customerPageSize = _customerPageSize;
            defaultPagesInGroup = _defaultPagesInGroup;
        }

        public override string DrawPager(string customerCssName)
        {
            //总分页数
            int allPage = Convert.ToInt32(Math.Ceiling((float)count / customerPageSize));
            defaultPagesInGroup = defaultPagesInGroup == 0 ? 10 : defaultPagesInGroup;
            //int groupCount = Convert.ToInt32(Math.Ceiling((float)allPage / defaultPagesInGroup));
            StringBuilder sb = new StringBuilder();
            sb.Append(DrawPagerJs(allPage, defaultPagesInGroup, customerPageSize, count));
            sb.Append("<div id=\"pager\">\r\n");

            //if (currentPgGroup > 1)
            //{
            //    sb.Append("<a href=\"?p=0&g=1\">&lt;&lt;</a>\r\n");
            //    sb.Append("<a href=\"?p=" + ((currentPgGroup - 1) * defaultPagesInGroup - 1) + "&g=" + currentPgGroup + "\">...</a>\r\n");
            //}

            //for (int i = 0; i < defaultPagesInGroup; i++)
            //{
            //    int index = (currentPgGroup - 1) * defaultPagesInGroup + i + 1;
            //    if (pageIndex == index - 1)
            //    {
            //        sb.Append("<span>" + index + "</span>\r\n");
            //    }
            //    else
            //    {
            //        sb.Append("<a href=\"?p=" + (index - 1) + "&g=" + currentPgGroup + "\">" + index + "</a>\r\n");
            //    }
            //    if (index == allPage)
            //    {
            //        break;
            //    }
            //}

            //if (currentPgGroup < groupCount)
            //{
            //    sb.Append("<a href=\"?p=" + currentPgGroup * defaultPagesInGroup + "&g=" + currentPgGroup + 1 + "\">...</a>\r\n");
            //    sb.Append("<a href=\"?p=" + (currentPgGroup - 1) + "&g=" + groupCount + "\">&gt;&gt;</a>\r\n");
            //}

            //sb.Append("<input type=\"text\" id=\"PageNumber\" name=\"PageNumber\" style=\"font:12px '宋体'; width:24px; height:14px;\" />&nbsp;<input type=\"button\" value=\"GO\" style=\"width:24px; height:20px;\" onclick=\"GoPage();\" />\r\n");

            sb.Append("</div>\r\n");
            sb.Append("<input type=\"hidden\" id=\"s\" name=\"s\" value=\"" + (pageIndex + 1) + "\" />\r\n");
            sb.Append("<input type=\"hidden\" id=\"g\" name=\"g\" value=\"" + (currentPgGroup == 0 ? currentPgGroup + 1 : currentPgGroup) + "\" />\r\n");
            sb.Append("<script type=\"text/javascript\">\r\n");
            sb.Append("  var p = document.getElementById(\"s\").value;");
            sb.Append("  var g = document.getElementById(\"g\").value;");
            sb.Append("  Draw(parseInt(g), parseInt(p), " + (PageSizeEnable ? 1 : 0) + "," + count + ");");
            sb.Append("</script>\r\n");
            sb.Append(DrawPagerCss());

            return sb.ToString();
        }
    }
}
